function [signal] = PAUSSignalSpectrumFromBin(bin_file_path,acq_parameter_file_path, modality, channels, valid_data_only, range, method)
% 从bin文件为输入

% bin_file_path            :  原始数据bin文件路径，每个bin文件第一帧为光声信号，其余帧为超声信号，此bin文件需要在前端界面选择
% acq_parameter_file_path  ： 原始数据采集参数文件（.csv文件）的路径，前期用不到可忽略，无需在界面选择
% modality      :  成像模态，0为超声，1为光声
% channels      :  数组，选中的通道，选中通道的数据会被依次提取出来
% valid_data_only  :  int, 显示通道全部数据还是仅显示有效数据，1表示仅显示有效数据，0表示显示全部数据
% range            :  二元数组，有效数据的范围。当valid_data_only为1时需要设置此二元数组
% signal           ：结构体，信号相关参数mat(光声/超声可使用相同的格式)，相关参数包括selectedchannels(选中的通道号),pointnumber（每个信号包括的点数），signals(所有通道信号的一维数组)，x-axis-label(x轴显示信息，如mm)，y-axis-label（y轴显示信息，如mm），x-axis-tick-label（x轴上的刻度值，如[-15，15]），y-axis-tick-label(y轴上的刻度值，如[0，30，50，70])
% signal_output = PAUSSignalSpectrumFromBin('E:\Projects\Test\Rawdata_FPGA1_Frame0.bin',1,0,[1 16 32 48 64],1,[8021 8520],'time_domain');
% signal_output = PAUSSignalSpectrumFromBin('E:\Projects\Test\Rawdata_FPGA1_Frame0.bin',1,1,[1 16 32 48 64],1,[4011 4510],'amplitude_frequency');

[Data_Rf, acqLen] = DataExtractFromBin(bin_file_path, 0);
Data_US_Rf = Data_Rf.US;
Data_PA_Rf = Data_Rf.PA;
FFTLength = 10e5;

if modality == 0
    for i=1:64
        for j=1:1
            cc0=double(Data_US_Rf(:,i,j));
            Alldata_US(:,i,j)=interp(cc0,5)';
        end
    end

    Alldata=mean(Alldata_US,3);

    if valid_data_only == 1
        analysis_signals_length = range(1):range(2); %有效信号范围
    elseif valid_data_only == 0
        analysis_signals_length = 1:length(Alldata);
        range = [1, length(Alldata)];
    end

    signals = Alldata(analysis_signals_length,channels);
    [~,q]=size(signals);

    addzerosfortestmultisignal=zeros((FFTLength-length(signals)),q);
    testmultisignal=[signals;addzerosfortestmultisignal];
    [m,n]=size(testmultisignal);
    fftresult=zeros(m,n);

    for Numofsignals=1:n
        fftresult(:,Numofsignals)=fft(testmultisignal(:,Numofsignals),length(testmultisignal(:,1)));
    end
    Th=0.02;

    for Numofsignals=1:n

        absY = (abs(fftresult(:,Numofsignals)).*2/(length(analysis_signals_length)));
        tempRef_Powerspectrum(:,Numofsignals)=10*log10((absY(1:FFTLength/2)));

        THETAY=angle((fftresult(:,Numofsignals)));

        thetadex=find(absY(1:FFTLength/2)<Th);
        THETAfixedY=THETAY(1:FFTLength/2);
        THETAfixedY(thetadex)=0;

        tempRef_THETAfixedY(:,Numofsignals)=unwrap(THETAfixedY);
    end

    Ref_Powerspectrum(:,1)=mean(tempRef_Powerspectrum(:,1:length(channels)),2);

    Ref_THETAfixedY(:,1)=mean(tempRef_THETAfixedY(:,1:length(channels)),2);


    % analysis_signals_length=8021:8520;  %信号范围

    signal.title = ['US_',method];
    switch method
        case 'time_domain'
            signal.signals = signals;
            XAxisLabel = xlabel('Point','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            signal.XAxisTickLabel = range;
            YAxisLabel = ylabel('Amplitude (a. u.)','fontname', 'times new roman','fontSize',30);
            min_signal_value = min(signal.signals);
            max_signal_value = max(signal.signals);
            delta = (max_signal_value - min_signal_value) * 0.05;
            signal.YAxisTickLabel = [min_signal_value-delta,  max_signal_value+delta];     %y轴刻度
        case 'hilbert_envelope'
            signal.signals = abs(hilbert(signals));
            XAxisLabel = xlabel('Point','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            signal.XAxisTickLabel = range;
            YAxisLabel = ylabel('Amplitude (a. u.)','fontname', 'times new roman','fontSize',30);
            min_signal_value = min(signal.signals);
            max_signal_value = max(signal.signals);
            delta = (max_signal_value - min_signal_value) * 0.05;
            signal.YAxisTickLabel = [min_signal_value-delta,  max_signal_value+delta];     %y轴刻度
        case 'amplitude_frequency'
            signal.signals = Ref_Powerspectrum(1:75000,1)-repmat(max(Ref_Powerspectrum(1:75000,1)),75000,1);
            XAxisLabel = xlabel('Frequency (MHz)','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            YAxisLabel = ylabel('NPSD (dB)','fontname', 'times new roman','fontSize',30);
            xlim([0 15]);
            signal.XAxisTickLabel = xlim;
            ylim([-15 0]);
            signal.YAxisTickLabel = ylim;
        case 'phase_frequency'
            signal.signals = Ref_THETAfixedY(1:75000,1);
            xlim([0 15]);
            signal.XAxisTickLabel = xlim;
            ylim([-40 -0]);
            signal.YAxisTickLabel = ylim;
            XAxisLabel = xlabel('Frequency (MHz)','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            YAxisLabel = ylabel('Phase (Angle)','fontname', 'times new roman','fontSize',30);
    end

    signal.XAxisLabel = XAxisLabel.String;
    signal.YAxisLabel = YAxisLabel.String;
    signal.pointnumber = length(analysis_signals_length);
    signal.selectedchannels = channels;
    close(gcf);

elseif modality == 1
    % ts=1/Fs;
    % Hd=myfilter;

    % save([save_path,'Data_PA_Rf_',datestr(now,30),'.mat'],'Data_PA_Rf');
    for i=1:64
        for j=1:1
            cc1=double(Data_PA_Rf(:,i,j));
            Alldata_PA(:,i,j)=interp(cc1,5)';
        end
    end
    % Alldata_before=filter(Hd,mean(Alldata_before,3));
    Alldata = mean(Alldata_PA,3);

    % PAR=40;z
    %     dirF=Fs/FFTLength
    %     dirF_MHz=Fs/FFTLength/1e6

    if valid_data_only == 1
        analysis_signals_length = range(1):range(2); %信号范围
    elseif valid_data_only == 0
        analysis_signals_length = 1:length(Alldata);
        range = [1, length(Alldata)];
    end

    signals = Alldata(analysis_signals_length,channels);
    [~,q]=size(signals);


    addzerosfortestmultisignal=zeros((FFTLength-length(signals)),q);
    testmultisignal=[signals;addzerosfortestmultisignal];
    [m,n]=size(testmultisignal);
    fftresult=zeros(m,n);

    for Numofsignals=1:n
        fftresult(:,Numofsignals)=fft(testmultisignal(:,Numofsignals),length(testmultisignal(:,1)));
    end
    Th=0.02;

    for Numofsignals=1:n

        absY = (abs(fftresult(:,Numofsignals)).*2/(length(analysis_signals_length)));
        tempRef_Powerspectrum(:,Numofsignals)=10*log10((absY(1:FFTLength/2)));

        THETAY=angle((fftresult(:,Numofsignals)));

        thetadex=find(absY(1:FFTLength/2)<Th);
        THETAfixedY=THETAY(1:FFTLength/2);
        THETAfixedY(thetadex)=0;

        tempRef_THETAfixedY(:,Numofsignals)=unwrap(THETAfixedY);
    end

    Ref_Powerspectrum(:,1)=mean(tempRef_Powerspectrum(:,1:length(channels)),2);

    Ref_THETAfixedY(:,1)=mean(tempRef_THETAfixedY(:,1:length(channels)),2);

    signal.title = ['PA_',method];
    switch method
        case 'time_domain'
            signal.signals = signals;
            XAxisLabel = xlabel('Point','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            signal.XAxisTickLabel = range;
            YAxisLabel = ylabel('Amplitude (a. u.)','fontname', 'times new roman','fontSize',30);
            min_signal_value = min(signal.signals);
            max_signal_value = max(signal.signals);
            delta = (max_signal_value - min_signal_value) * 0.05;
            signal.YAxisTickLabel = [min_signal_value-delta,  max_signal_value+delta];     %y轴刻度
        case 'hilbert_envelope'
            signal.signals = abs(hilbert(signals));
            XAxisLabel = xlabel('Point','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            signal.XAxisTickLabel = range;
            YAxisLabel = ylabel('Amplitude (a. u.)','fontname', 'times new roman','fontSize',30);
            min_signal_value = min(signal.signals);
            max_signal_value = max(signal.signals);
            delta = (max_signal_value - min_signal_value) * 0.05;
            signal.YAxisTickLabel = [min_signal_value-delta,  max_signal_value+delta];     %y轴刻度
        case 'amplitude_frequency'
            signal.signals = Ref_Powerspectrum(1:75000,1)-repmat(max(Ref_Powerspectrum(1:75000,1)),75000,1);
            XAxisLabel = xlabel('Frequency (MHz)','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            YAxisLabel = ylabel('NPSD (dB)','fontname', 'times new roman','fontSize',30);
            xlim([0 15]);
            signal.XAxisTickLabel = xlim;
            ylim([-15 0]);
            signal.YAxisTickLabel = ylim;
        case 'phase_frequency'
            signal.signals = Ref_THETAfixedY(1:75000,1);
            xlim([0 15]);
            signal.XAxisTickLabel = xlim;
            ylim([-25 -0]);
            signal.YAxisTickLabel = ylim;
            XAxisLabel = xlabel('Frequency (MHz)','fontname', 'times new roman','fontSize',30);%设置坐标轴的标签字号大小(20)
            YAxisLabel = ylabel('Phase (Angle)','fontname', 'times new roman','fontSize',30);
    end
    signal.XAxisLabel = XAxisLabel.String;
    signal.YAxisLabel = YAxisLabel.String;
    signal.pointnumber = length(analysis_signals_length);
    signal.selectedchannels = channels;
    close(gcf);

end

end